package com.ccjk.beusoft.thirdlib.plugin3;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by CCJK on 2017/12/2.
 */

public class BasePluginActivity extends AppCompatActivity implements PluginActivityInterface {

    public Activity proxyActivity;
    PluginPackage pluginPackage;
    private int from = Const.FROM_INTERNAL;


    public void bind(Activity activity, PluginPackage pluginPackage) {
        this.proxyActivity = activity;
        this.pluginPackage = pluginPackage;

    }

    public void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            from = savedInstanceState.getInt(Const.KEY_ACTIVITY_FROM, Const.FROM_INTERNAL);
        }
        if (from == Const.FROM_INTERNAL) {
            super.onCreate(savedInstanceState);
            proxyActivity = this;
        }

    }

    public void setContentView(int layoutResID) {
        if (from == Const.FROM_INTERNAL)
            super.setContentView(layoutResID);
        else proxyActivity.setContentView(layoutResID);
    }

    public void setContentView(View view) {
        if (from == Const.FROM_INTERNAL) super.setContentView(view);
        else proxyActivity.setContentView(view);
    }

    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (from == Const.FROM_INTERNAL) super.setContentView(view, params);
        else proxyActivity.setContentView(view, params);
    }

    public void addContentView(View view, ViewGroup.LayoutParams params) {
        if (from == Const.FROM_INTERNAL) super.addContentView(view, params);
        else proxyActivity.addContentView(view, params);
    }


    @Override
    public <T extends View> T findViewById(int id) {
        if (from == Const.FROM_INTERNAL)
            return super.findViewById(id);
        else return proxyActivity.findViewById(id);
    }

    public Intent getIntent() {
        if (from == Const.FROM_INTERNAL) return super.getIntent();
        else return proxyActivity.getIntent();
    }


    public ClassLoader getClassLoader() {
        if (from == Const.FROM_INTERNAL)
            return super.

                    getClassLoader();
        else return proxyActivity.getClassLoader();
    }


    public Resources getResources() {
        if (from == Const.FROM_INTERNAL) return super.

                getResources();
        else return proxyActivity.getResources();
    }


    public AssetManager getAssets() {
        if (from == Const.FROM_INTERNAL) return super.

                getAssets();
        else return proxyActivity.getAssets();
    }


    public String getPackageName() {
        if (from == Const.FROM_INTERNAL) return super.

                getPackageName();
        else return proxyActivity.getPackageName();
    }


    @NonNull
    public LayoutInflater getLayoutInflater() {
        if (from == Const.FROM_INTERNAL) return super.

                getLayoutInflater();
        else return proxyActivity.getLayoutInflater();
    }


    @NonNull
    public MenuInflater getMenuInflater() {

        if (from == Const.FROM_INTERNAL) return super.

                getMenuInflater();
        else return proxyActivity.getMenuInflater();
    }

    public SharedPreferences getSharedPreferences(String name, int mode) {
        if (from == Const.FROM_INTERNAL) return super.

                getSharedPreferences(name, mode);
        else return proxyActivity.getSharedPreferences(name, mode);
    }


    public Context getApplicationContext() {
        if (from == Const.FROM_INTERNAL) return super.

                getApplicationContext();
        else return proxyActivity.getApplicationContext();
    }


    public WindowManager getWindowManager() {
        if (from == Const.FROM_INTERNAL) return super.

                getWindowManager();
        else return proxyActivity.getWindowManager();
    }


    public Window getWindow() {
        if (from == Const.FROM_INTERNAL) return super.

                getWindow();
        else return proxyActivity.getWindow();
    }


    public Object getSystemService(@NonNull String name) {
        if (from == Const.FROM_INTERNAL) return super.

                getSystemService(name);
        else return proxyActivity.getSystemService(name);
    }


    public void onStart() {
        if (from == Const.FROM_INTERNAL) super.onStart();
    }

    public void onRestart() {
        if (from == Const.FROM_INTERNAL) super.onRestart();

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (from == Const.FROM_INTERNAL) super.onActivityResult(requestCode, resultCode, data);
    }

    public void onResume() {
        if (from == Const.FROM_INTERNAL) super.onResume();
    }

    public void onPause() {
        if (from == Const.FROM_INTERNAL) super.onPause();
    }

    public void onStop() {
        if (from == Const.FROM_INTERNAL) super.onStop();
    }

    public void onDestroy() {
        if (from == Const.FROM_INTERNAL) super.onDestroy();
    }

    public void onSaveInstanceState(Bundle outState) {
        if (from == Const.FROM_INTERNAL) super.onSaveInstanceState(outState);
    }

    public void onNewIntent(Intent intent) {
        if (from == Const.FROM_INTERNAL) super.onNewIntent(intent);
    }

    public void onRestoreInstanceState(Bundle savedInstanceState) {
        if (from == Const.FROM_INTERNAL) super.onRestoreInstanceState(savedInstanceState);
    }

    public boolean onTouchEvent(MotionEvent event) {
        return from == Const.FROM_INTERNAL && super.onTouchEvent(event);
    }


    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return from == Const.FROM_INTERNAL && super.onKeyUp(keyCode, event);
    }


    public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
        if (from == Const.FROM_INTERNAL) super.onWindowAttributesChanged(params);
    }

    public void onWindowFocusChanged(Boolean hasFocus) {
        if (from == Const.FROM_INTERNAL) super.onWindowFocusChanged(hasFocus);
    }

    public void onBackPressed() {
        if (from == Const.FROM_INTERNAL) super.onBackPressed();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        return from == Const.FROM_INTERNAL && super.onCreateOptionsMenu(menu);
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        return from == Const.FROM_INTERNAL && super.onOptionsItemSelected(item);
    }


    public void finish() {
        if (from == Const.FROM_INTERNAL) super.finish();
        else proxyActivity.finish();
    }
}
